/*
 * @(#)GSProductInfoDownServlet.java   
 *
 * Project: CUPSecureBM
 * 
 * Modify Information:
 *  Author          Date                Description
 * ==============   =================   =====================
 *  yonggangguo     2005-12-29             Create this file
 *  Tuthmos Liu     2008-06-16           增加图件号
 * Copyright notice:
 *   Copyright (c) 2001-2005 Beijing HiTRUST Technology Co., Ltd.
 *   1808 Room, Science & Technology Building, No. 9 South Street,
 *   Zhong Guan Cun, Haidian District, Beijing ,100081, China
 *   All rights reserved.
 *
 *   This software is the confidential and proprietary information of
 *   HiTRUST.COM, Inc. ('Confidential Information').  You shall not
 *   disclose such Confidential Information and shall use it only in
 *   accordance with the terms of the license agreement you entered
 *   into with HiTRUST.
 */

package com.hitrust.gs.action;

import java.io.OutputStream;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import system.base.Product;
import system.base.ProductType;
import system.base.ProductUpdatePrice;
import system.base.ProductUpdatePriceSearchEvent;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.service.GSBaseManager;
import com.hitrust.gs.util.DefineDic;

import framework.core.DBConnection;
import framework.core.ObjectList;

public class GSProductUpdatePriceDownServlet extends HttpServlet {

    public void init(ServletConfig aConfig) throws ServletException {
        super.init(aConfig);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        response.setContentType("application/excel;charset=UTF-16");
        response.setHeader("Content-disposition", "attachment; filename=productUpdatePrice.xls");
        response.setHeader("Content-Description", "产品调价信息");

       
        DBConnection dbcon = null;
        OutputStream tOutputStream = null;
        try {
            dbcon = DBConnection.open();
            tOutputStream = response.getOutputStream();
         
            	 if(request.getSession().getAttribute("ProductUpdatePriceSearchEvent") instanceof ProductUpdatePriceSearchEvent){
            		 ProductUpdatePriceSearchEvent event = (ProductUpdatePriceSearchEvent)request.getSession().getAttribute("ProductUpdatePriceSearchEvent");
            		 ObjectList list = ProductUpdatePrice.fetch().search(dbcon, event.toConditions());
                     writeExcel(list, tOutputStream);
            	 }
       
            
        } catch (Exception e) {
            GSLogWriter.error(e.getMessage());
        } finally {
            try {
                if (tOutputStream != null) {
                    tOutputStream.close();
                }
                dbcon.close();
            } catch (Exception e) {
            }
        }
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        doPost(request, response);
    }

    private void writeExcel(ObjectList aList, OutputStream tOutputStream) {
        DBConnection con = null;
        try {
                con = DBConnection.open();
                HSSFWorkbook tWb = new HSSFWorkbook();
                HSSFSheet tSheet = tWb.createSheet();
                tWb.setSheetName(0, "产品浏览", HSSFCell.ENCODING_UTF_16);
                HSSFRow tRow = tSheet.createRow((short) 0);
                createCell(tWb, tRow, (short) 0, "集中编码", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 1, "产品名称", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 2, "区域", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 3, "规格", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 4, "型号", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 5, "供应商", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 6, "图件号", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 7, "启用日期", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 8, "调价前价格", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 9, "调价后价格", HSSFCellStyle.ALIGN_CENTER);
                
                createCell(tWb, tRow, (short) 10, "是否启用", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 11, "审批状态", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 12, "创建人", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 13, "创建时间", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 14, "审批人", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 15, "审批时间", HSSFCellStyle.ALIGN_CENTER);
          
                for (int i = 0; i < aList.size(); i++) {
                    tRow = tSheet.createRow((short) (i + 1));
                    ProductUpdatePrice obj = (ProductUpdatePrice) aList.get(i);
                	Product product = Product.fetch(con,
        					obj.getProductId());
                 	ProductType ptype = ProductType.fetchByProductId(con,
                			product.getId());
                    createCell(tWb, tRow, (short) 0, obj.getConcentricityCode());
                    createCell(tWb, tRow, (short) 1, product.viewName(con));
                    createCell(tWb, tRow, (short) 2, ptype == null ? "" : DefineDic
							.getProductType(ptype.getProductType() + ""));
                    createCell(tWb, tRow, (short) 3, product.viewSpec(con));
                    createCell(tWb, tRow, (short) 4, product.viewModel(con));
                    createCell(tWb, tRow, (short) 5, product.viewProviderCode(con));
                    createCell(tWb, tRow, (short) 6, product.viewDrawingPieceCode(con));
                    createCell(tWb, tRow, (short) 7, obj.getEnabledTime());
                    createCell(tWb, tRow, (short) 8, obj.getBeforePrice()+"");
                    createCell(tWb, tRow, (short) 9, obj.getAfterPrice()+"");
                    createCell(tWb, tRow, (short) 10, GSBaseManager.getStatusName(obj.getStatus()));
                    createCell(tWb, tRow, (short) 11, GSBaseManager.getAuditingStatusName(obj.viewAuditingStatus(con)));
                    createCell(tWb, tRow, (short) 12, obj.getCreateUser());
                    createCell(tWb, tRow, (short) 13, obj.getCreateTime());
                    createCell(tWb, tRow, (short) 14, obj.getAuditingUser());
                    createCell(tWb, tRow, (short) 15, obj.getAuditingTime());
               }
                tWb.write(tOutputStream);
                tOutputStream.flush();
      
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                con.close();
            } catch (Exception e) {
            }
        }
    }

    private void createCell(HSSFWorkbook aWb, HSSFRow aRow, short column, String aValue) {
        HSSFCell tCell = aRow.createCell(column);
        tCell.setEncoding(HSSFCell.ENCODING_UTF_16);
        tCell.setCellValue(aValue);
    }

    private void createCell(HSSFWorkbook aWb, HSSFRow aRow, short column, String aValue, short aAlign) {
        HSSFCell tCell = aRow.createCell(column);
        tCell.setEncoding(HSSFCell.ENCODING_UTF_16);
        tCell.setCellValue(aValue);
        HSSFCellStyle tCellStyle = aWb.createCellStyle();
        tCellStyle.setAlignment(aAlign);
        tCell.setCellStyle(tCellStyle);
    }
}
